<?php
declare(strict_types=1);

namespace app\common\model\delivery;

use app\common\model\Area;
use app\common\model\base\BaseModel;
use app\common\model\User;

class Customer extends BaseModel
{
    protected $append=['user'];

    const CUSTOMERTYPE=[
        'company'=>'公司',
        'family'=>'住户'
    ];

    public function area()
    {
        return $this->hasOne(Area::class,'id','area_id')->field('id,mergename');
    }

    public function channel()
    {
        return $this->hasOne(Channel::class,'id','channel_id');
    }

    public function gird()
    {
        return $this->hasOne(ChannelGird::class,'id','gird_id');
    }

    public function seller()
    {
        return $this->hasOne(Seller::class,'id','seller_id');
    }

    public function commission()
    {
        return $this->hasOne(SellerCustomer::class,'customer_id','id');
    }

    public function goods()
    {
        return $this->hasMany(CustomerGoods::class,'customer_id','id');
    }

    public function address()
    {
        return $this->hasMany(CustomerAddress::class,'customer_id','id')->where('is_delete',0);
    }

    public function getSettleTypeAttr($data)
    {
        if($data){
            return explode(',',$data);
        }
        return [];
    }

    public static function parseGoods($goods,$channel)
    {
        $channelGoods=$channel->goods;
        $r=[];
        foreach ($goods as $value){
            foreach ($channelGoods as $cg){
                foreach ($cg['spec'] as $spec){
                    if($cg['id']==$value['goods_id'] && $spec['id']==$value['spec_id']){
                        $r[]=[
                            'goods_id'=>$cg['id'],
                            'spec_id'=>$spec['id'],
                            'goods_name'=>$cg['name'],
                            'goods_img'=>$cg['img'],
                            'goods_des'=>$cg['des'],
                            'spec_name'=>$spec['name'],
                            'spec_price'=>$value['price'],
                            'spec_unit'=>$spec['unit'],
                        ];
                    }
                }
            }
        }
        return $r;
    }

    public function getAutoOrderRuleAttr($data,$row)
    {
        if($data){
            $rules=json_decode($data,true);
            $r=[];
            foreach ($rules as $goods_id=>$arr) {
                $b=false;
                foreach ($arr as $key=>$value) {
                    $arr[$key]=intval($value);
                    if($arr[$key]){
                        $b=true;
                    }
                }
                if($b){
                    $r[$goods_id]=$arr;
                }
            }
            if(!empty($r)){
                return $r;
            }
        }
        return [];
    }

    public function getUserAttr($data,$row)
    {
        if(!isset($row['user_id']) || !$row['user_id']){
            return [];
        }
        return User::whereIn('id',$row['user_id'])->cache('customer-user-'.$row['user_id'],3600*24)->field('id,nickname,avatar')->select();
    }
}
